<template>
  <div class="w-full max-w-md slide-in">
    <div class="card">
      <div class="text-center mb-8">
        <h2 class="text-2xl font-bold text-gray-900">
          注册账号
        </h2>
        <p class="mt-2 text-sm text-gray-600">
          已有账号？
          <router-link to="/auth/login" class="font-medium text-indigo-600 hover:text-indigo-500">
            立即登录
          </router-link>
        </p>
      </div>

      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-position="top"
        @submit.prevent="handleSubmit"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="form.username"
            :prefix-icon="User"
            placeholder="请输入用户名"
          />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input
            v-model="form.password"
            type="password"
            :prefix-icon="Lock"
            show-password
            placeholder="请输入密码"
          />
        </el-form-item>

        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            v-model="form.confirmPassword"
            type="password"
            :prefix-icon="Lock"
            show-password
            placeholder="请再次输入密码"
          />
        </el-form-item>

        <el-form-item label="邮箱" prop="email">
          <el-input
            v-model="form.email"
            :prefix-icon="Message"
            placeholder="请输入邮箱"
          />
        </el-form-item>

        <el-form-item label="姓名" prop="name">
          <el-input
            v-model="form.name"
            :prefix-icon="User"
            placeholder="请输入姓名"
          />
        </el-form-item>

        <el-form-item label="角色" prop="role">
          <el-radio-group v-model="form.role" class="flex gap-4">
            <el-radio label="ROLE_TEACHER" class="!flex items-center">
              <el-icon class="mr-1"><Notebook /></el-icon>
              教师
            </el-radio>
            <el-radio label="ROLE_STUDENT" class="!flex items-center">
              <el-icon class="mr-1"><Reading /></el-icon>
              学生
            </el-radio>
          </el-radio-group>
        </el-form-item>

        <div class="mt-8">
          <el-button
            type="primary"
            class="w-full !h-11 !text-base"
            :loading="loading"
            @click="handleSubmit"
          >
            {{ loading ? '注册中...' : '注册' }}
          </el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock, Message, Notebook, Reading } from '@element-plus/icons-vue'
import type { FormInstance } from 'element-plus'
import { useAuthStore } from '@/stores/auth'
import type { RegisterForm } from '@/types/user'

const router = useRouter()
const authStore = useAuthStore()
const formRef = ref<FormInstance>()
const loading = ref(false)

const form = reactive<RegisterForm>({
  username: '',
  password: '',
  confirmPassword: '',
  email: '',
  role: 'ROLE_STUDENT',
  name: ''
})

const validatePass = (_rule: any, value: string, callback: Function) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== form.password) {
    callback(new Error('两次输入密码不一致'))
  } else {
    callback()
  }
}

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 4, max: 20, message: '长度在 4 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, validator: validatePass, trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ],
  name: [
    { required: true, message: '请输入姓名', trigger: 'blur' },
    { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
  ],
  role: [
    { required: true, message: '请选择角色', trigger: 'change' }
  ]
}

async function handleSubmit() {
  if (!formRef.value) return

  await formRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        const success = await authStore.register({
          username: form.username,
          password: form.password,
          email: form.email,
          role: form.role,
          name: form.name
        })
        if (success) {
          ElMessage.success('注册成功')
          await router.push('/auth/login')
        }
      } catch (error: any) {
        ElMessage.error(error.message || '注册失败，请稍后重试')
      } finally {
        loading.value = false
      }
    }
  })
}
</script> 